package com.journaldev.spring;

import java.io.IOException;
import java.util.Set;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.journaldev.spring.model.City;
import com.journaldev.spring.model.District;
import com.journaldev.spring.service.CityService;
import com.journaldev.spring.service.DistrictService;

@Controller
public class CityController {
	
	private CityService cityService;
	
	@Autowired(required=true)
	@Qualifier(value="cityService")
	public void setCityService(CityService ps){
		this.cityService = ps;
	}
	
	@RequestMapping(value = {"/cities"}, method = RequestMethod.GET)
	public String listCities(Model model) {
		model.addAttribute("city", new City());
		model.addAttribute("listCities", this.cityService.listCity());
		return "city";
	}
	
	//For add and update person both
	@RequestMapping(value= "/city/add", method = RequestMethod.POST)
	public String addCity(@ModelAttribute("city") City p){
		
		if(p.getId() == 0){
			//new person, add it
			this.cityService.addCity(p);
		}else{
			//existing person, call update
			this.cityService.updateCity(p);
		}
		
		return "redirect:/cities";
		
	}
	
	@RequestMapping("/city/remove/{id}")
    public String removeCity(@PathVariable("id") int id){
		
        this.cityService.removeCity(id);
        return "redirect:/cities";
    }
 
    @RequestMapping("/city/edit/{id}")
    public String editCity(@PathVariable("id") int id, Model model){
        model.addAttribute("city", this.cityService.getCityById(id));
        model.addAttribute("listPersons", this.cityService.listCity());
        return "city";
    }
    
	
	@RequestMapping(value="/city/getDistrictsByCity/{id}", method=RequestMethod.GET)
    public @ResponseBody String getDistrictsByCity(@PathVariable("id") int idCity) throws JsonGenerationException, JsonMappingException, IOException{
        City targetCity = this.cityService.getCityById(idCity);
        if(targetCity != null) {
        	Set<District> districtList = targetCity.getDisTrict();
        	
        	JsonArray ja = new JsonArray();
        	for (District s : districtList) {
        		JsonObject jo = new JsonObject();
        		jo.addProperty("id", s.getId());
        		jo.addProperty("districtName", s.getDistrictName());

        		ja.add(jo);
        	}
        	
        	JsonObject mainObj = new JsonObject();
    		mainObj.add("result", ja);

        	System.out.println(mainObj.toString());
        	return mainObj.toString();
        }
        return "";
    }

}
